/**
 * AjLog - The AspectJ based logging facade
 * 
 * Copyright (C) 2009 - 2010, Marko Umek. All rights reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.ajlog.core.message.segment;

import org.ajlog.core.profiling.IProfilingResult;

/**
 * AbstractMessageSegment is abstract base class for providing a simple
 * implementation for almost every (dynamic) message segment implementation. <br>
 * 
 * @author Marko Umek
 * 
 */
public abstract class AbstractMessageSegment
    implements IMessageSegment {

    /**
     * Constructor
     * 
     */
    public AbstractMessageSegment() {
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.ajlog.core.message.IMessageSegment#buildDynamicMessage(java.lang.
     * StringBuilder, java.lang.Object[])
     */
    public final void buildDynamicMessage(final StringBuilder messageBuilder, final Object[] paramValues) {
        this.handleIndependentDynamicValues(messageBuilder);
        this.handleParameters(messageBuilder, paramValues);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.ajlog.core.message.IMessageSegment#buildDynamicMessage(java.lang.
     * StringBuilder, java.lang.Object[], java.lang.Object)
     */
    public final void buildDynamicMessage(final StringBuilder messageBuilder, final Object[] paramValues, final Object retval) {
        this.buildDynamicMessage(messageBuilder, paramValues);
        this.handleReturnValue(messageBuilder, retval);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.ajlog.core.message.IMessageSegment#buildDynamicMessage(java.lang.
     * StringBuilder, java.lang.Object[], java.lang.Object,
     * org.ajlog.core.message.profiling.ProfilingResult)
     */
    public final void buildDynamicMessage(final StringBuilder messageBuilder, final Object[] paramValues, final Object retval,
            final IProfilingResult profilingResult) {
        this.buildDynamicMessage(messageBuilder, paramValues, retval);
        this.handleProfiling(profilingResult);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.ajlog.core.message.IMessageSegment#buildDynamicMessage(java.lang.
     * StringBuilder, java.lang.Object[], java.lang.Exception)
     */
    public final void buildDynamicMessage(final StringBuilder messageBuilder, final Object[] paramValues,
            final Exception exception) {
        this.buildDynamicMessage(messageBuilder, paramValues);
        this.handleException(messageBuilder, exception);
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.ajlog.core.message.IMessageSegment#buildDynamicMessage(java.lang.
     * StringBuilder, java.lang.Object[], java.lang.Exception,
     * org.ajlog.core.message.profiling.ProfilingResult)
     */
    public final void buildDynamicMessage(final StringBuilder messageBuilder, final Object[] paramValues,
            final Exception exception, final IProfilingResult profilingResult) {
        this.buildDynamicMessage(messageBuilder, paramValues, exception);
        this.handleProfiling(profilingResult);
    }

    /**
     * handleIndependentDynamicValues will be used for dynamic message building. <br>
     * <br>
     * <b>Note:</b> Implement this method if the implementation is not
     * interested in any actual parameter(s), return value or exception.
     * 
     * @param messageBuilder
     *            the message builder instance.
     */
    protected void handleIndependentDynamicValues(final StringBuilder messageBuilder) {
    }

    /**
     * handleParameters will be used for dynamic message building. <br>
     * <br>
     * <b>Note:</b> Implement this method if the implementation is interested in
     * any actual parameter(s).
     * 
     * @param messageBuilder
     *            the message builder instance.
     */
    protected void handleParameters(final StringBuilder messageBuilder, final Object[] paramValues) {
    }

    /**
     * Internal method will be used for dynamic message building. <br>
     * <br>
     * <b>Note:</b> Override this method if the implementation must react on the
     * return value.
     * 
     * @param messageBuilder
     *            the message builder instance.
     * @param retval
     *            the return value
     * 
     */
    protected void handleReturnValue(final StringBuilder messageBuilder, final Object retval) {
    }

    /**
     * Internal method will be used for dynamic message building. <br>
     * <br>
     * <b>Note:</b> Override this method if the implementation must react on the
     * exception.
     * 
     * @param messageBuilder
     *            the message builder instance.
     * @param exception
     *            the exception
     * 
     */
    protected void handleException(final StringBuilder messageBuilder, final Exception exception) {
    }

    /**
     * Internal method will be used for dynamic message building. <br>
     * <br>
     * <b>Note:</b> Override this method if the implementation must handle the
     * profiling result.
     * 
     * @param profilingResult
     *            the profiling result.
     */
    protected void handleProfiling(final IProfilingResult profilingResult) {
    }
}